package m09;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

import javax.swing.JList;
import javax.swing.JButton;

import m06.GameScreen;
import m07.GamePanel;
import m07.Player;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class LoadSavedGame extends JFrame implements Serializable{
	private static final long serialVersionUID = 8814383342193493340L;
	private JPanel contentPane;
	private GamePanel newPanel;

	/**
	 * Create the frame.
	 */
	public LoadSavedGame(final ArrayList<Player> playerList) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 292, 318);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		String[] nameArray = new String[playerList.size()];
		for (int i = 0; i<playerList.size(); i++){
			nameArray[i] = playerList.get(i).getName();
		}
		
		final JList list = new JList(nameArray);
		list.setBounds(11, 11, 266, 212);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		contentPane.add(list);
		
		JButton btnBegin = new JButton("Begin!");
		btnBegin.addActionListener(new ActionListener() {
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent e) {
				String useMe = (String)(list.getSelectedValue());
				ArrayList<Integer> origData;
				try{
					FileInputStream fileIn = new FileInputStream(useMe + "_map.ser");
		            ObjectInputStream in = new ObjectInputStream(fileIn);
		            origData = (ArrayList<Integer>) in.readObject();
		            in.close();
		            fileIn.close();
		            //GameScreen newScreen = new GameScreen(panel);
		            //newScreen.setVisible(true);
		            Global.currentPlayer = playerList.get(list.getSelectedIndex());
		            
		            
		            
		            newPanel = new GamePanel();
		            
		           //newPanel.setMapPanel(origPanel);
		            newPanel.activateGoods();
		            newPanel.getMapPanel().setTechNum(origData.get(0));
		            newPanel.getMapPanel().setCurrentSys(origData.get(1));
		            newPanel.waterGood.updatePrice(origData.get(0));
		            newPanel.furGood.updatePrice(origData.get(0));
		            newPanel.foodGood.updatePrice(origData.get(0));
		            newPanel.oreGood.updatePrice(origData.get(0));
		            newPanel.gamesGood.updatePrice(origData.get(0));
		            newPanel.fireGood.updatePrice(origData.get(0));
		            newPanel.medsGood.updatePrice(origData.get(0));
		            newPanel.machineGood.updatePrice(origData.get(0));
		            newPanel.narcGood.updatePrice(origData.get(0));
		            newPanel.robotGood.updatePrice(origData.get(0));
		            		            
		            //CUR SS
		            
		            newPanel.activateSolarSystem();
		            newPanel.updateSellButtons();
		            newPanel.updateBuyButtons();
		            newPanel.updatePlayer();
		            newPanel.updateMoney();
		            newPanel.updateFuel();
		            newPanel.updateLabels();
		            newPanel.setTrader(Global.currentPlayer.getTrader());
		            newPanel.setPilot(Global.currentPlayer.getPilot());
		            newPanel.setFighter(Global.currentPlayer.getFighter());
		            newPanel.setEngineer(Global.currentPlayer.getEngineer());
		            GameScreen newScreen = new GameScreen(newPanel);
		            newScreen.setVisible(true);
		            
				}
				catch (Exception ex){
					ex.printStackTrace();
					JOptionPane.showMessageDialog(null, "There was an error recovering your game.  New game!");
				}
			}
		});
		btnBegin.setBounds(90, 261, 117, 29);
		contentPane.add(btnBegin);
		setVisible(false);
	}
}
